package ioc

import (
	"github.com/redis/go-redis/v9"
	"github.com/spf13/viper"
	clientv3 "go.etcd.io/etcd/client/v3"
	"time"
)

func InitEtcd(cmd redis.Cmdable) clientv3.KV {
	client := InitEtcdClient()
	kv := clientv3.NewKV(client)
	//InitEtcdSetting(cmd)
	return kv
}
func InitEtcdClient() *clientv3.Client {
	addr := viper.GetString("etcd.addr")
	configEtcd := clientv3.Config{
		Endpoints:   []string{addr}, // Etcd 服务器地址
		DialTimeout: 5 * time.Second,
	}
	client, err := clientv3.New(configEtcd)

	if err != nil {
		panic(err)
	}
	return client
}

//func InitEtcdSetting(cmd redis.Cmdable) {
//	etcdEndpoints := []string{"http://127.0.0.1:12379"} // Etcd 服务器地址
//	keyToWatch := "/webook"
//	config := clientv3.Config{
//		Endpoints:   etcdEndpoints,
//		DialTimeout: 5 * time.Second,
//	}
//	client, err := clientv3.New(config)
//	if err != nil {
//		// 错误处理
//	}
//	var smsConfig conf.SettingConfig
//	watcher := client.Watch(context.Background(), keyToWatch, clientv3.WithPrefix())
//	go func() {
//		for resp := range watcher {
//			for _, event := range resp.Events {
//				err = yaml.Unmarshal(event.Kv.Value, &smsConfig)
//				InitTencentSms(cmd, &smsConfig)
//			}
//		}
//	}()
//}
